Skip to content

Add TryFrom<&str> For InfoKind#261

Closed
cathay4t wants to merge 1 commit into
rust-netlink:mainfrom
cathay4t:main
Closed

Add TryFrom<&str> For InfoKind#261
cathay4t wants to merge 1 commit into
rust-netlink:mainfrom
cathay4t:main

Conversation

@cathay4t

Copy link
Copy Markdown
Member

No description provided.

Signed-off-by: Gris Ge <cnfourt@gmail.com>
@codecov

codecov Bot commented May 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.66%. Comparing base (1fb4bd7) to head (1e72266).
⚠️ Report is 45 commits behind head on main.

Files with missing lines Patch % Lines
src/link/link_info/infos.rs 0.00% 35 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #261      +/-   ##
==========================================
- Coverage   68.10%   67.66%   -0.45%     
==========================================
  Files         144      146       +2     
  Lines       10103    10316     +213     
==========================================
+ Hits         6881     6980      +99     
- Misses       3222     3336     +114     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the TryFrom<&str> trait for the InfoKind enum to allow for conversion from string slices. The reviewer suggests that since the conversion is infallible due to a catch-all match arm, it is more idiomatic to implement the From<&str> trait instead. This would simplify the code by removing unnecessary Result wrapping and provide a more standard interface for the conversion.

Comment on lines +305 to +344
impl TryFrom<&str> for InfoKind {
type Error = DecodeError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
Ok(match s {
DUMMY => Self::Dummy,
IFB => Self::Ifb,
BRIDGE => Self::Bridge,
TUN => Self::Tun,
NLMON => Self::Nlmon,
VLAN => Self::Vlan,
VETH => Self::Veth,
VXLAN => Self::Vxlan,
BOND => Self::Bond,
IPVLAN => Self::IpVlan,
IPVTAP => Self::IpVtap,
MACVLAN => Self::MacVlan,
MACVTAP => Self::MacVtap,
GRETAP => Self::GreTap,
IP6GRETAP => Self::GreTap6,
IPIP => Self::IpIp,
IP6TNL => Self::Ip6Tnl,
SIT => Self::SitTun,
GRE => Self::GreTun,
IP6GRE => Self::GreTun6,
VTI => Self::Vti,
VRF => Self::Vrf,
GTP => Self::Gtp,
IPOIB => Self::Ipoib,
WIREGUARD => Self::Wireguard,
XFRM => Self::Xfrm,
MACSEC => Self::MacSec,
HSR => Self::Hsr,
GENEVE => Self::Geneve,
NETKIT => Self::Netkit,
VXCAN => Self::Vxcan,
_ => Self::Other(s.to_owned()),
})
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the conversion from &str to InfoKind is infallible (the catch-all _ arm handles all cases by returning Self::Other), it is more idiomatic to implement the From<&str> trait instead of TryFrom<&str>. TryFrom is intended for conversions that can fail, whereas From is preferred for guaranteed conversions. This change also simplifies the code by removing the unnecessary Result wrapping. Additionally, once this trait is implemented, the Parseable::parse implementation for InfoKind can be refactored to use it, reducing code duplication.

impl From<&str> for InfoKind {
    fn from(s: &str) -> Self {
        match s {
            DUMMY => Self::Dummy,
            IFB => Self::Ifb,
            BRIDGE => Self::Bridge,
            TUN => Self::Tun,
            NLMON => Self::Nlmon,
            VLAN => Self::Vlan,
            VETH => Self::Veth,
            VXLAN => Self::Vxlan,
            BOND => Self::Bond,
            IPVLAN => Self::IpVlan,
            IPVTAP => Self::IpVtap,
            MACVLAN => Self::MacVlan,
            MACVTAP => Self::MacVtap,
            GRETAP => Self::GreTap,
            IP6GRETAP => Self::GreTap6,
            IPIP => Self::IpIp,
            IP6TNL => Self::Ip6Tnl,
            SIT => Self::SitTun,
            GRE => Self::GreTun,
            IP6GRE => Self::GreTun6,
            VTI => Self::Vti,
            VRF => Self::Vrf,
            GTP => Self::Gtp,
            IPOIB => Self::Ipoib,
            WIREGUARD => Self::Wireguard,
            XFRM => Self::Xfrm,
            MACSEC => Self::MacSec,
            HSR => Self::Hsr,
            GENEVE => Self::Geneve,
            NETKIT => Self::Netkit,
            VXCAN => Self::Vxcan,
            _ => Self::Other(s.to_owned()),
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant